
<template>
  <div class="app-container">
 
 <div class="handle-box">
      <router-link :to="'/example/mycreate/' ">
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    class="handle-plus mr10"
                    
                >添加文章</el-button>
      </router-link>
                <el-input v-model="query.title" placeholder="文章标题" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch()">搜索</el-button>

                <el-button type="primary" icon="el-icon-refresh" @click="getList()">重置</el-button>
            </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span >{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="300px" label="标题">
        <template slot-scope="{ row }">
          <router-link :to="'/example/edit/' + row.id" class="link-type">
            <span>{{ row.title }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="类型">
        <template slot-scope="scope">
          <span>{{ scope.row.articleType }}</span>
        </template>
      </el-table-column>
      <el-table-column width="120px" align="center" label="来源">
        <template slot-scope="scope">
          <span>{{ scope.row.source }}</span>
        </template>
      </el-table-column>

      <el-table-column width="100px" label="权重">
        <template slot-scope="scope">
          <svg-icon
            v-for="n in +scope.row.weight"
            :key="n"
            icon-class="star"
            class="meta-item__icon"
          />
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="Status" width="110">
        <template slot-scope="{ row }">
          <el-tag :type="row.status | statusFilter">
            {{ row.active }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column width="180px" align="center" label="发布时间">
        <template slot-scope="scope">
          <span>{{
            scope.row.createdAt | parseTime("{y}-{m}-{d} {h}:{i}")
          }}</span>
        </template>
      </el-table-column>

     <el-table-column align="center" label="操作" width="120">
        <template slot-scope="scope">
          <router-link :to="'/example/edit/' + scope.row.id">
            <el-button type="primary" size="small" icon="el-icon-edit">
              编辑
            </el-button>
          </router-link>
          <el-button size="small" icon="el-icon-delete" @click="handleDelete(scope.row.id)" > 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total >0" :total="total" :page.sync="listQuery.index" :limit.sync="listQuery.max" @pagination="getList" />
  </div>

</template>
<script>


import { fetchList, createArticle, deleteArticle, fetchPv } from '@/api/myarticle'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import qs from 'qs';

export default {
  name: 'ArticleList',
  components: { Pagination },
   filters: {
 statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger",
      };
      return statusMap[status];
    }
   },
  data() {
    
    return {
         query: {
                title: '',
                id: 0
            },
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        index: 1,
        max: 10
      },
      dQuery: {
        id: '0'
      },
      SQuery: {
        keyword: 0,
        index: 1,
        max: 1
      },
      temp: {
        body: '',
        source: '',
        title: '',
        imgUrl: '',
        sourceUrl: '',
        keyword: ''
      }
    }
  },
  created() {
    this.getList()

  },
  methods: {
    getList() {
      fetchList(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data;
        this.total = this.list.length
         console.log(this.total)
      })
    },

    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createArticle(this.temp).then(() => {
            this.list.unshift(this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj

      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      
    },
   
    handleDelete() {
      console.log(this.dQuery)
      deleteArticle(this.dQuery).then((response) => {
        console.log(response.data)
      })
    },

    handleFetchPv() {
      fetchPv(this.SQuery).then(response => {
        this.pvData = response.data.pvData
        this.dialogPvVisible = true
      })
    },
 refreshData() {
      this.getData(1);
    },
    handleSearch() {
   
      const _this = this;
      const keyword= _this.query.title;
      console.log(keyword);
      this.listLoading = true;
      
      queryArticle(keyword).then((response) => {
  
       this.list = response.data;
        //this.total = response.data.total;
        this.listLoading = false;
        console.log(this.list)
        
        
      });

    }

  }
}
</script>

